<template>
  <div id="app">
     <el-container class="home-container">
      <el-header>
        <div>
          <img src="../assets/logo.png">
          <span>后台管理系统</span>
        </div>
        <el-button type="info" @click="loginout">退出</el-button>
      </el-header>
      <el-container>
        <!--侧边栏菜单-->
        <el-aside :width="iscollapse?'64px':'200px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            :unique-opened="false"
            :collapse="iscollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath">
            <!--一级菜单-->
            <el-submenu :index="item.id+''" v-for="item in menuData" :key="item.id">
              <!--一级菜单模板区域-->
              <template slot="title">
                <!--图标-->
                <i :class="item.icon"></i>
                <!--文本-->
                <span>{{item.name}}</span>
              </template>
              <!--二级菜单-->
              <el-menu-item :index="'/'+childItem.path" v-for="childItem in item.children" :key="childItem.id"
              @click="saveNavState('/'+childItem.path)">
                <!--二级菜单模板区域-->
                <template slot="title">
                  <!--图标-->
                  <i class="el-icon-menu"></i>
                  <!--文本-->
                  <span>{{childItem.name}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!--主内容-->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      // 左侧导航数据
      menuData: [],
      // 控制折叠
      iscollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  methods: {
    loginout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('api/Permission')
      // console.log(res)
      if (JSON.parse(res).meta.status !== 200) return this.$msg.error(JSON.parse(res).meta.msg)
      // console.log(JSON.parse(res))
      this.menuData = JSON.parse(res).data
    },
    // 点击按钮控制菜单的折叠或者打开
    toggleCollapse () {
      this.iscollapse = !this.iscollapse
    },
    // 保存链接的激活状态 用于选中菜单的高亮
    saveNavState (path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.home-container{height: 100%;}
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  color: white;
  div{
    display: flex;
    align-items: center;
    padding:0px;
    img{
      height: 50px;
      width: 50px;
      border-radius: 50%;
      background-color: #eee;
    }
    span{
      margin-left: 10px;
    }
  }
}
.el-menu{border-right: 0px;}
.el-aside{background-color: #333744;  }
.el-main{background-color: #eaedf1;}
.iconfont{margin-right: 10px;}
.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
